package main

import (
	"archive/zip"
	"fmt"
	"io"
	"io/ioutil"
	"math/big"
	"math/rand"
	"os"
	"path"
	"path/filepath"
	"regexp"
	"server/base"
	"strconv"
	"time"

	"github.com/cheggaaa/pb/v3"
	"github.com/tidwall/gjson"
)

// 测长整数
func test1() {
	const n = 1000000000000000000
	var a uint64 = n
	a += 1
	fmt.Println("a:", a)
}

// 长整数除法
func test2() {
	z, b := new(big.Int).SetString("10000000002000000000", 10)
	fmt.Println("z:", z.String(), b)
	z.Div(z, big.NewInt(1000000000))
	fmt.Println("z2:", z.String())
}

// gjson
func test3() {
	text := `{"a":100000000000000000000}`
	r := gjson.Parse(text)
	fmt.Println("string():", r.Get("a").String())
	fmt.Println("int():", r.Get("a").Int())
}

// 截取字符串
func trunc(s string, prelen, suflen int) string {
	n := len(s)
	if n <= prelen+suflen {
		return s
	}
	o := s[:prelen]
	o += "..."
	o += s[n-suflen:]
	return o
}

func test4() {
	s := "1.2799999999999999E-4"
	f, _ := strconv.ParseFloat(s, 64)
	fmt.Printf("f: %.02f\n", f)
}

func test5() {
	s := "1[1.2]"
	reg := regexp.MustCompile(`^(\d+)\[([\w.]+)\]$`)
	p := reg.FindStringSubmatch(s)
	fmt.Println("p:", p[2])
}

func test6() {
	p := "D:\\PS4游戏\\热门\\Z\\ZS4-1-战神4中文本体V1.00-[6.72][7.02][CUSA07413][35.1G].pkg"
	p = base.CleanPath(p)
	d, f := filepath.Split(p)
	//d, f := path.Split("D:\\PS4游戏\\热门\\Z\\ZS4-1-战神4中文本体V1.00-[6.72][7.02][CUSA07413][35.1G].pkg")
	fmt.Println(d)
	fmt.Println(f)
	dst := path.Join("G:", f)
	fmt.Println("dst:", dst)
	joinf := filepath.Join("G:\\", "xxxx.txt")
	fmt.Println("join path:", joinf)
	_, err := os.Create("/Users/cat/d/e.txt")
	base.Raise(err)
}

func test7() {
	count := 1000
	//bar := pb.StartNew(count)
	//bar := pb.Default.Start(count)
	//bar := pb.Simple.Start(count)
	bar := pb.Full.Start(count)

	for i := 0; i < count; i++ {
		bar.Increment()
		time.Sleep(time.Millisecond)
	}
	bar.Finish()
}

// qsort
func test8() {
	a := []int{2, 3, 4, 1, 5}
	rand.Seed(time.Now().Unix())
	for i := 0; i < 20; i++ {
		rand.Shuffle(len(a), func(i, j int) {
			a[i], a[j] = a[j], a[i]
		})
		fmt.Println("a0:", a)
		SortInts(a)
		fmt.Println("a1:", a)
	}
}

// zip reader
func test9() {
	z, err := zip.OpenReader("/Users/cat/d/dst.zip")
	base.Raise(err)
	defer z.Close()
	for _, f := range z.File {
		fmt.Println("f:", f.Name)
		r, err := f.Open()
		base.Raise(err)
		b, err := ioutil.ReadAll(r)
		fmt.Println("b:", string(b))
		r.Close()
	}
}

// zip writer
func test10() {
	f, err := os.OpenFile("/Users/cat/d/go1.zip", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
	base.Raise(err)
	defer f.Close()
	w := zip.NewWriter(f)
	o, err := w.Create("go1/a.txt")
	base.Raise(err)
	n, err := io.WriteString(o, "hello")
	base.Raise(err)
	w.Close()
	fmt.Println("finish:", n)
}

func test11() {
	m := make(map[string]int)
	m["a"]++
	fmt.Println("m:", m)
}

func main() {
	test11()
}
